<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

use App\Models\User;
use App\Models\GoodsBrowse;
use Carbon\Carbon;



Route::namespace('User')->prefix('zbsuser')->name('zbsuser.')->group(function(){

    // 客户登陆
    Route::resource('login','LoginController');
    Route::get('logout','LoginController@logout')->name('login.logout');
    Route::post('upImage','LoginController@upImage')->name('login.upImage');



    Route::middleware(['auth'])->group(function(){

        Route::get('/','IndexController@index')->name('index.index');
        Route::get('/console','IndexController@console')->name('index.console');
        Route::get('/userinfo','IndexController@userInfo')->name('index.userinfo');
        Route::patch('index/{user}/edit','IndexController@update')->name('index.update');
        Route::post('upImage','IndexController@upImage')->name('index.upImage');
        //

        // 商品列表
        Route::get('goods/goodsBrowse','GoodsController@goodsBrowse')->name('goods.goodsBrowse');
        Route::post('goods/goodsBrowseData','GoodsController@goodsBrowseData')->name('goods.goodsBrowseData');
        Route::get('goods/show_data/{goods}','GoodsController@show_data')->name('goods.show_data');
        Route::resource('goods','GoodsController');

        // 支付管理
        Route::get('alipay/wecha_index','AliPayController@wecha_index')->name('alipay.wecha_index');
        Route::get('alipay/wecha_store','AliPayController@wecha_index')->name('alipay.wecha_store');
        //Route::post('alipay/store/{userInfo}','AliPayController@store')->name('alipay.store');
        Route::resource('alipay','AliPayController')->only(['index','store','update']);


        // 订单列表
        Route::resource('order','OrderController');




    });




});





Route::get('indexs',function(){


    dd('fff',GoodsBrowse::whereBetween('created_at',[Carbon::parse('2020-05-24') , Carbon::parse('2020-05-24 23:59:59')])->get());
//    dd(GoodsBrowse::where('created_at' , now())->update(['pagevies'=> 0,'show_num'=> 0]));

    $sum_browse = User::find(88)->goods_browse()
        ->whereDate('created_at',now())->where('pagevies','!=','0')
        ->select([
            'user_id','goods_id',
            \DB::raw('SUM(pagevies) as pagevies'),
            \DB::raw('SUM(show_num) as show_num'),
            \DB::raw('SUM(order_num) as order_num'),
            \DB::raw('SUM(deal_num) as deal_num'),
        ])->groupBy('user_id','goods_id')->get();

//    dd($sum_browse , $sum_browse->toArray());



    foreach ($sum_browse as $item) {
        if ($userBrowseTotal = $item->user->userBrowseTotal()->where('goods_id' , $item->goods_id)->whereDate('created_at',now())->first()){
            $userBrowseTotal->increment('pagevies' , $item->pagevies);
            $userBrowseTotal->increment('order_num' , $item->order_num);
            $userBrowseTotal->increment('deal_num' , $item->deal_num);
            $userBrowseTotal->increment('show_num' , $item->show_num);
        }else{

            $item->user->userBrowseTotal()->create($item->toArray());
//            $item->user->userBrowseTotal()->create([
//                'user_id'  => $item->user_id,
//                'goods_id' => $item->goods_id,
//                'pagevies' => $item->pagevies,
//                'order_num'=> $item->order_num,
//                'deal_num' => $item->deal_num,
//                'show_num' => $item->show_num,
//            ]);
        }
    }

//    dd('aaa' , $a->count() , $a , strtotime('2020-05-20'));

    $users = User::query()->find(100);

    echo Carbon::parse('today')->toDateTimeString() . '<br>'; //2016-10-15 00:00:00
    echo Carbon::parse('yesterday')->toDateTimeString() . '<br>'; //2016-10-14 00:00:00
    echo Carbon::parse('tomorrow')->toDateTimeString() . '<br>'; //2016-10-16 00:00:00
    echo Carbon::parse('2 days ago')->toDateTimeString() . '<br>'; //2016-10-13 20:49:53
    echo Carbon::parse('+3 days')->toDateTimeString() . '<br>'; //2016-10-18 20:49:53
    echo Carbon::parse('+2 weeks')->toDateTimeString() . '<br>'; //2016-10-29 20:49:53
    echo Carbon::parse('+4 months')->toDateTimeString() . '<br>'; //2017-02-15 20:49:53
    echo Carbon::parse('-1 year')->toDateTimeString() . '<br>'; //2015-10-15 20:49:53
    echo Carbon::parse('next wednesday')->toDateTimeString() . '<br>'; //2016-10-19 00:00:00
    echo Carbon::parse('last friday')->toDateTimeString() . '<br>'; //2016-10-14 00:00:00


    echo '<br>';
    echo Carbon::now()->addDays(25) . '<br>'; //2016-11-09 14:00:01
    echo Carbon::now()->addWeeks(3) . '<br>'; //2016-11-05 14:00:01
    echo Carbon::now()->addHours(25) . '<br>'; //2016-10-16 15:00:01
    echo Carbon::now()->subHours(2) . '<br>'; //2016-10-15 12:00:01
    echo Carbon::now()->addHours(2)->addMinutes(12) . '<br>'; //2016-10-15 16:12:01
    echo Carbon::now()->modify('+15 days') . '<br>'; //2016-10-30 14:00:01
    echo Carbon::now()->modify('-2 days') . '<br>'; //2016-10-13 14:00:01


    dd(Carbon::parse('-1 year')->toDateTimeString()); //2015-10-15 20:49:53);

    $goods_browse = $users[0]->goods_browse()->where('goods_id' , 1)->whereDate('created_at',now())->get()->toArray();
    shuffle($goods_browse);// 打乱查询出来的数组
    dd($goods_browse[0] , collect($goods_browse));
    $a = 0.3333333333333333;

    dd($a , number_format(ceil($a) , 0 , '.' , '') , number_format($a), intval($a));



//    dd($users[0]->goods,
//        $users[0]->goods_browse()->where('goods_id' , 1)->whereDate('created_at','2020-05-14')->get());

    dd(Carbon::parse($users[0]->goods_browse->where('goods_id' , 1)[0]->created_at->toDateString())->diffInDays(),
        $users[0]->goods_browse->where('goods_id' , 1)[0]->created_at->toDateString(),
        $users[0]->goods_browse->where('goods_id' , 1)[0]->created_at->toDateString(),
        $users[0]->goods_browse->where('goods_id' , 1)[0],
        $users[0]->goods_browse->where('goods_id' , 1)[0]->whereDate('created_at',now()));


    dd($users[0]->goods_browse->where('goods_id' , 1)[0]->created_at->toDateString());
    dd($users[0]->goods_browse->where('goods_id' , 1)[0]->created_at->toDateString());
    dd($users[0]->goods_browse->where('goods_id' , 1)[0]->created_at);

});
